# coding:utf8

"""
使python代码更加优雅
"""
# 列表推导方式
lists = [x * x for x in range(10) if x % 3 == 0]
print(lists)

# 字典的默认值
dicts = {
    "name": "jack",
    "age": 18
}
dicts["companyCount"] = dicts.get("companyCount", 0) + 1
print(dicts)

# for .. else ...语句
for i in range(5):
    if i == 5:
        print('find num 5')
        break
else:
    print("not find num 5")

# enumerate 可以一次将序列的索引和值取出
array = [1, 2, 3, 4, 5, 10, 11]
for i, j in enumerate(array):
    print(i, j)

keys = ['Name', 'Sex', 'Age']
values = ['Tim', 'Male', 23]
# zip来创建字典
print(zip(keys, values))
print(dict(zip(keys, values)))

# 德摩根定律
# （非p）且 （非q） == 非（p 或 q）
a = None
b = None

if not a and not b:
    print("a,b")
if not (a or b):
    print("都不存在")

# 非（p 且 q ）==（ 非 p ）或（ 非 q ）

if not a or not b:
    print("a,b")
if not (a and b):
    print("都不存在")


class UserCollection:

    def __init__(self, users):
        self._users = users

    def __len__(self):
        print(self._users)
        return len(self._users)

    def __bool__(self):
        return bool(self._users)

    def __add__(self, other):
        return other
    # ['piglei', 'raymond']


users = UserCollection(users="jack")
if users:
    print(users.__bool__())
    print(users.__add__(['piglei', 'raymond']))
    print(users.__len__())
    print("There's some users in collection!")


# all(seq)：仅当 seq 中所有对象都为布尔真时返回 True，否则返回 False
def all_numbers_gt_10_2(numbers):
    return bool(numbers) and all(n > 10 for n in numbers)


# any（）只要 seq 中任何一个对象为布尔真就返回 True，否则返回 False
def any_numbers_gt_10_2(numbers):
    return bool(numbers) and any(n > 10 for n in numbers)


print(all_numbers_gt_10_2(array))
print(any_numbers_gt_10_2(array))

# 判断与None值得比较 用is
# and 运算的优先级要高于or的
